import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller/update_password_controller.dart';

/// 修改密码页面
class UpdatePasswordScreen extends StatelessWidget {
  UpdatePasswordScreen({Key? key}) : super(key: key);

  final UpdatePasswordController controller = Get.put(UpdatePasswordController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () => Get.back(),
        ),
        centerTitle: true,
        title: const Text('重置密码', style: TextStyle(color: Colors.black, fontSize: 22, fontWeight: FontWeight.bold)),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Column(
          children: [
            const SizedBox(height: 32),
            // 原密码输入框
            Obx(() => _buildPwdField(
                  hint: '请输入原密码',
                  value: controller.oldPassword.value,
                  onChanged: (v) => controller.oldPassword.value = v,
                  visible: controller.oldPwdVisible.value,
                  onEyeTap: () => controller.oldPwdVisible.value = !controller.oldPwdVisible.value,
                )),
            const SizedBox(height: 20),
            // 新密码输入框
            Obx(() => _buildPwdField(
                  hint: '请输入新密码',
                  value: controller.newPassword.value,
                  onChanged: (v) => controller.newPassword.value = v,
                  visible: controller.newPwdVisible.value,
                  onEyeTap: () => controller.newPwdVisible.value = !controller.newPwdVisible.value,
                )),
            const SizedBox(height: 20),
            // 确认新密码输入框
            Obx(() => _buildPwdField(
                  hint: '确认新密码',
                  value: controller.confirmPassword.value,
                  onChanged: (v) => controller.confirmPassword.value = v,
                  visible: controller.confirmPwdVisible.value,
                  onEyeTap: () => controller.confirmPwdVisible.value = !controller.confirmPwdVisible.value,
                )),
            const SizedBox(height: 40),
            // 确认按钮
            SizedBox(
              width: double.infinity,
              height: 56,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFFFF7B7B),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(28),
                  ),
                ),
                onPressed: controller.submit,
                child: const Text('确认', style: TextStyle(fontSize: 20, color: Colors.white)),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建密码输入框
  Widget _buildPwdField({
    required String hint,
    required String value,
    required ValueChanged<String> onChanged,
    required bool visible,
    required VoidCallback onEyeTap,
  }) {
    return Container(
      decoration: BoxDecoration(
        color: const Color(0xFFF5F5F5),
        borderRadius: BorderRadius.circular(28),
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              obscureText: !visible,
              onChanged: onChanged,
              decoration: InputDecoration(
                hintText: hint,
                border: InputBorder.none,
                contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
                hintStyle: const TextStyle(color: Colors.grey, fontSize: 18),
              ),
              style: const TextStyle(fontSize: 18),
            ),
          ),
          IconButton(
            icon: Icon(visible ? Icons.visibility : Icons.visibility_off, color: Colors.black54),
            onPressed: onEyeTap,
          ),
        ],
      ),
    );
  }
}
